﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlaceHR.Processor.Processor.Vacation.Steps
{
    class FinalizeSalaryStepCalculator : StepCalculatorBase<VacationProcessorContext, IList<VacationBase>>
    {
        public override void Calculate(VacationProcessorContext context, IList<VacationBase> entity)
        {
            if (entity == null || entity.Count < 1)
            {
                return;
            }

            foreach (var item in entity)
            {
                if (item != null && (item.InitialHours.HasValue || item.MothIncrement.HasValue || item.FinalHours.HasValue))
                {
                    //将同类型假期的IsCurrent设置为0
                    var vacationBaseList = context.HRDBContext.VacationBase.Where(p => p.EmployeeID == context.CurrentEmployee.EmployeeID
                                                        && p.IsCurrent
                                                        && p.VacationTypeCode != null
                                                        && p.VacationTypeCode.Equals(item.VacationTypeCode, StringComparison.OrdinalIgnoreCase)).ToList();
                    if (vacationBaseList != null && vacationBaseList.Count > 0)
                    {
                        vacationBaseList.ForEach(p => p.IsCurrent = false);
                    }

                    // 将最新的一条假期设置为IsCurrent
                    item.IsCurrent = true;
                    if (item.VacationBaseId < 1)
                    {
                        context.HRDBContext.VacationBase.AddObject(item);
                    }
                }
            }

            context.CurrentEngine.HRDBContext.SaveChanges();
        }

    }
}
